ld a,15 dec aA would now be 14, since we've subtracted 1 from 15. Likewise,
ld a,15 inc awould give us 16, by adding 1 to a. Dec and inc can be used with all sorts of things. The 8-bit registers, 16-bit registers, regular old numbers, and even (hl), the byte that is at memory address hl points to. So here's just a sampling of the legal ways you can use the two:
inc b | dec hl | inc $c0 |
dec (hl) | inc de | dec d |
inc 15 | dec bc | inc %10011011 |
ld a,15 add a,6A would now be equal to 21. Likewise,
ld hl,300 ld de,200 add hl,dewould cause hl to be equal to 500. So here's a table of some legal things we can add:
add a,4 | add hl,hl | add a,b |
add hl,de | add a,(hl) | add a,$5f |
add hl,bc | add a,d | add a,%10010011 |
Now to look a sub. Sub is even more limited--we can only work with a, not even hl. But like add, we can subtract the 8-bit registers, numbers, and (hl) from a. So this:
ld a,15 sub 6would result in a being equal to 9. Note that it's not sub a,6, but just sub 6.
We can sla and sra any of the 8-bit registers and (hl). So let's say we're shifting a for now. We're going to work in binary, because it's perfect for visualizing how this shifting works. Let's do this:
ld a,%00101010 sla aThis would shift all the bits over 1 position towards the left. So now a would be equal to %01010100. Now if we think about decimal numbers, it sure looks like a was multiplied by 10. When we multiply things by 10, everything's shifted over to the left and we add a zero at the end, which is exactly what happened here. But remember that we're in binary, and %10 is actually the binary equivalent of 2. So we've actually multiplied a by 2. Now convering to decimal, %00101010 = 42 and %01010100 = 84. Pretty cool. Now to go the reverse direction:
ld a,%00101010 sra aThis would cause a to be equal to %00010101 = 21. It just divides a by 2.
So now you can add, subtract, and multiply and divide by 2. As I said a little earlier, in the next column, we're going to learn all about the carry flag, and then dive into some more byte shifts and rotations.